import { _decorator, Button, instantiate, Label, Node, NodePool, Tween, tween, UIOpacity, UITransform, v3, Widget } from 'cc';
import { Ab_DataTransfer_bA } from '../../../A-FRAME/A-LIB/lib.b.data';
import { Ab_BaseUI_bA } from '../../../A-FRAME/component/ui.base';
import { Ab_AudioManager_bA } from '../../manager/audio-manager';
import Ab_Config_bA from '../../manager/config.manager';
import Ab_GameDC_bA from '../../manager/data.game.center';
import Ab_GameAssets_bA from '../../manager/game.assets';
import { Ab_Float_bA } from './float';
import { Ab_Popup_bA } from './popup';
import { Ab_Guide_bA } from './guide';
const { ccclass, property } = _decorator;

@ccclass('Ab_HomeUI_bA')
export class Ab_HomeUI_bA extends Ab_BaseUI_bA {

	private Ab_ui_top_bA: UITransform;
	private Ab_va_top_bA: abi.Ab_cc_bA.Ab_VecAssist_bA;

	private Ab_ui_symbol_bA: UITransform;
	private Ab_ui_diamond_bA: UITransform;
	private Ab_lbl_symbol_bA: Label;
	private Ab_lbl_diamond_bA: Label;
	private Ab_icon_symbol_bA: Node;
	private Ab_icon_diamond_bA: Node;

	private Ab_symbol_demo_bA: Node;
	private Ab_symbol_pool_bA: NodePool;

	protected Ab_initAfterOnLoad_bA(){
		this.Ab_ui_top_bA = this.node.getChildByName('top').getComponent(UITransform);
		this.Ab_va_top_bA = new abi.Ab_cc_bA.Ab_VecAssist_bA(this.Ab_ui_top_bA.node);

		this.Ab_ui_symbol_bA = this.Ab_ui_top_bA.node.getChildByName('symbol').getComponent(UITransform);
		this.Ab_ui_diamond_bA = this.Ab_ui_top_bA.node.getChildByName('diamond').getComponent(UITransform);
		this.Ab_lbl_symbol_bA = this.Ab_ui_symbol_bA.node.getChildByName('lbl-value').getComponent(Label);
		this.Ab_lbl_diamond_bA = this.Ab_ui_diamond_bA.node.getChildByName('lbl-value').getComponent(Label);
		this.Ab_icon_symbol_bA = this.Ab_ui_symbol_bA.node.getChildByName('icon');
		this.Ab_icon_diamond_bA = this.Ab_ui_diamond_bA.node.getChildByName('icon');

		this.Ab_symbol_demo_bA = this.Ab_ui_symbol_bA.node.getChildByName('amount');
		this.Ab_symbol_pool_bA = new NodePool();

		this.Ab_symbol_pool_bA.put(this.Ab_symbol_demo_bA);

		this.Ab_renderLocalUI_bA();
	}

	protected start(){
		let wgt = this.node.getComponent(Widget);
		if(wgt) wgt.updateAlignment();

		this.Ab_va_top_bA.Ab_getPosition_bA('Ab_center_bA', 'Ab_over_top_bA');
		this.Ab_ui_top_bA.node.setPosition(this.Ab_va_top_bA.Ab_vec_bA);

		let reg = Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_Trigger_bA, 'Ab_LiveCommentsProgress_bA', ()=>{
			let [f, t] = Ab_GameDC_bA.Ab_Trigger_bA.Ab_LiveCommentsProgress_bA.split(',').map(v=>parseInt(v));
			let p = f/t;
			if(p>=1){
				Ab_DataTransfer_bA.Ab_cancel_bA(reg);
				this.node.active = false;
			}
		}, false);

		let has_new_task_done = false;
		has_new_task_done = Ab_Config_bA.Ab_taskRemove_bA.filter(el=>!Ab_GameDC_bA.Ab_checkTask_bA('remove', el.id) && el.count <= Ab_GameDC_bA.Ab_User_bA.Ab_remove_count_bA).length > 0;
		has_new_task_done = has_new_task_done || Ab_Config_bA.Ab_taskRound_bA.filter(el=>!Ab_GameDC_bA.Ab_checkTask_bA('round', el.round) && el.round < Ab_GameDC_bA.Ab_User_bA.Ab_round_bA).length > 0;

		this.Ab___btn_map___bA.get('task').node.getChildByName('hint-point').active = has_new_task_done;
	}

	protected Ab__click_event__bA: { [name: string]: (this: Ab_HomeUI_bA, button: Button) => void; } = {
		setting(btn){
			btn.interactable = false;
			Ab_Popup_bA.Ab_Setting_bA().then(()=>btn.interactable = true);
		},
		task(btn){
			btn.interactable = false;
			Ab_Popup_bA.Ab_Task_bA().then(()=>btn.interactable = true);
		},
		rank(btn){
			btn.interactable = false;
			Ab_Popup_bA.Ab_Rank_bA().then(()=>btn.interactable = true);
			btn.node.getChildByName('hint-point').active = false;
			Ab_GameDC_bA.Ab_User_bA.Ab_open_rank_bA = Date.now();
		},
		free(btn){
			btn.interactable = false;
			Ab_Popup_bA.Ab_Free_bA().then(()=>btn.interactable = true);
		},
		diamond(btn){
			btn.interactable = false;
			Ab_Float_bA.Ab_Hint_bA(Ab_GameAssets_bA.Ab_getLangText_bA(67)).then(()=>btn.interactable = true);
		}
	};

	protected Ab_listening_bA(){

		this.Ab_lbl_symbol_bA.string = Ab_Config_bA.Ab_symbolString_bA(Ab_GameDC_bA.Ab_User_bA.Ab_symbol_bA);
		this.Ab_lbl_diamond_bA.string = Ab_GameDC_bA.Ab_User_bA.Ab_diamond_bA.toString();
		let node_hint_task = this.Ab___btn_map___bA.get('task').node.getChildByName('hint-point');

		let lbl_pool = new NodePool();
		let lbl_offset = v3(0, 35, 0);

		let hilit_token_symbol: string;
		let hilit_token_diamond: string;

		let list = [
			Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_Trigger_bA, 'Ab_UIHome_bA', (_, show)=>{
				if(_==show) return void 0;
				if(show) this.Ab_enter_bA();
				else this.Ab_leave_bA();
			}, false),
			Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_Trigger_bA, 'Ab_CHANGE_SYMBOL_bA', async (_, num)=>this.Ab_changeSymbol_bA(num), false),
			Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_Trigger_bA, 'Ab_CHANGE_DIAMOND_bA', async (_, num)=>{
				Ab_GameDC_bA.Ab_User_bA.Ab_diamond_bA += num;
				let vec = Ab_GameDC_bA.Ab_reapDiamondVec_bA;
				if(vec) Ab_Float_bA.Ab_Reap_bA(this.Ab_icon_diamond_bA, abi.Ab_number_bA.Ab_random_bA(6, 8), vec).then(()=>{
					this.Ab_lbl_diamond_bA.string = Ab_GameDC_bA.Ab_User_bA.Ab_diamond_bA.toString();
					let lbl = (lbl_pool.get() ?? instantiate(this.Ab_lbl_diamond_bA.node)).getComponent(Label);

					lbl.string = '+'+num;
					lbl.node.scale.set(0.8, 0.8, 1);
					lbl.node.active = true;
					lbl.node.setParent(this.Ab_lbl_diamond_bA.node.parent);
					lbl.node.setPosition(this.Ab_lbl_diamond_bA.node.position);

					tween(lbl.node).by(0.6, {position: lbl_offset}).delay(0.6)
					.call(()=>lbl_pool.put(lbl.node)).start();
				});
				else this.Ab_lbl_diamond_bA.string = Ab_GameDC_bA.Ab_User_bA.Ab_diamond_bA.toString();
			}, false),
			Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_Trigger_bA, 'Ab_HILIT_SYMBOL_bA', (_, light)=>{
				if(light) hilit_token_symbol = Ab_Float_bA.Ab_heightLight_bA([this.Ab_ui_symbol_bA.node]);
				else Ab_Float_bA.Ab_releaseHILIT_bA(hilit_token_symbol);
			}, false),
			Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_Trigger_bA, 'Ab_HILIT_DIAMOND_bA', (_, light)=>{
				if(light) hilit_token_diamond = Ab_Float_bA.Ab_heightLight_bA([this.Ab_ui_diamond_bA.node]);
				else Ab_Float_bA.Ab_releaseHILIT_bA(hilit_token_diamond);
			}, false),
			Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_User_bA, 'Ab_round_bA', ()=>{
				let has_new_task_done = Ab_Config_bA.Ab_taskRound_bA.filter(el=>!Ab_GameDC_bA.Ab_checkTask_bA('round', el.round) && el.round < Ab_GameDC_bA.Ab_User_bA.Ab_round_bA).length > 0;
				has_new_task_done = has_new_task_done || Ab_Config_bA.Ab_taskRemove_bA.filter(el=>!Ab_GameDC_bA.Ab_checkTask_bA('remove', el.id) && el.count <= Ab_GameDC_bA.Ab_User_bA.Ab_remove_count_bA).length > 0;

				node_hint_task.active = has_new_task_done;
			}, false),
			Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_User_bA, 'Ab_remove_count_bA', ()=>{
				let has_new_task_done = Ab_Config_bA.Ab_taskRemove_bA.filter(el=>!Ab_GameDC_bA.Ab_checkTask_bA('remove', el.id) && el.count <= Ab_GameDC_bA.Ab_User_bA.Ab_remove_count_bA).length > 0;
				has_new_task_done = has_new_task_done || Ab_Config_bA.Ab_taskRound_bA.filter(el=>!Ab_GameDC_bA.Ab_checkTask_bA('round', el.round) && el.round < Ab_GameDC_bA.Ab_User_bA.Ab_round_bA).length > 0;

				node_hint_task.active = has_new_task_done;
			}, false),
			Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_User_bA, 'Ab_task_round_bA', ()=>{
				let has_new_task_done = Ab_Config_bA.Ab_taskRound_bA.filter(el=>!Ab_GameDC_bA.Ab_checkTask_bA('round', el.round) && el.round < Ab_GameDC_bA.Ab_User_bA.Ab_round_bA).length > 0;
				has_new_task_done = has_new_task_done || Ab_Config_bA.Ab_taskRemove_bA.filter(el=>!Ab_GameDC_bA.Ab_checkTask_bA('remove', el.id) && el.count <= Ab_GameDC_bA.Ab_User_bA.Ab_remove_count_bA).length > 0;

				node_hint_task.active = has_new_task_done;
			}, false),
			Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_User_bA, 'Ab_task_remove_bA', ()=>{
				let has_new_task_done = Ab_Config_bA.Ab_taskRemove_bA.filter(el=>!Ab_GameDC_bA.Ab_checkTask_bA('remove', el.id) && el.count <= Ab_GameDC_bA.Ab_User_bA.Ab_remove_count_bA).length > 0;
				has_new_task_done = has_new_task_done || Ab_Config_bA.Ab_taskRound_bA.filter(el=>!Ab_GameDC_bA.Ab_checkTask_bA('round', el.round) && el.round < Ab_GameDC_bA.Ab_User_bA.Ab_round_bA).length > 0;

				node_hint_task.active = has_new_task_done;
			}, false),
		];
		return list;
	}

	private Ab_enterAfter_bA(){
		if(Ab_GameDC_bA.Ab_User_bA.Ab_round_bA>=4) Ab_GameDC_bA.Ab_Trigger_bA.Ab_UILuckyBox_bA = true;
		this.Ab_initGuide_bA();
	}

	private Ab_enter_bA(duration?: number){
		this.node.active = true;
		duration = duration ?? 0.5;

		this.Ab_va_top_bA.Ab_getPosition_bA('Ab_center_bA', 'Ab_top_bA');
		if(abi.Ab_cc_bA.Ab_is_long_screen_bA) this.Ab_va_top_bA.Ab_vec_bA.add3f(0, -100, 0);

		Tween.stopAllByTarget(this.Ab_ui_top_bA.node);
		tween(this.Ab_ui_top_bA.node)
		.to(duration, {position: this.Ab_va_top_bA.Ab_vec_bA}, {easing: 'cubicOut'})
		.call(()=>this.Ab_enterAfter_bA())
		.start();
	}

	private Ab_leave_bA(duration?: number){
		duration = duration ?? 0.5;

		this.Ab_va_top_bA.Ab_getPosition_bA('Ab_center_bA', 'Ab_over_top_bA');

		Tween.stopAllByTarget(this.Ab_ui_top_bA.node);
		tween(this.Ab_ui_top_bA.node)
		.to(duration, {position: this.Ab_va_top_bA.Ab_vec_bA}, {easing: 'cubicIn'})
		.call(()=>this.node.active = false)
		.start();

		if(Ab_GameDC_bA.Ab_User_bA.Ab_round_bA) Ab_GameDC_bA.Ab_Trigger_bA.Ab_UILuckyBox_bA = false;
	}

	private Ab_renderLocalUI_bA(){
		let sframe_symbol = Ab_GameAssets_bA.Ab_getCounTexture_bA(0);
		abi.Ab_cc_bA.Ab_skinPeeler_bA(this.Ab_ui_symbol_bA.node.getChildByName('icon'), sframe_symbol, {width: true});
		abi.Ab_cc_bA.Ab_skinPeeler_bA(this.Ab_symbol_demo_bA.getChildByName('icon'), sframe_symbol, {width: true});
		this.Ab_ui_symbol_bA.node.getChildByName('lbl-button').getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(5);
		this.Ab_ui_symbol_bA.node.getChildByName('light').active = false;
		let bubble = this.Ab_ui_symbol_bA.node.getChildByName('bubble');
		bubble.getChildByName('lbl-value').getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(52);
		bubble.active = false;

		this.Ab___btn_map___bA.get('rank').node.getChildByName('hint-point').active = !abi.Ab_time_bA.Ab_sameDay_bA(Date.now(), Ab_GameDC_bA.Ab_User_bA.Ab_open_rank_bA);
	}

	private async Ab_changeSymbol_bA(amount: number){
		Ab_GameDC_bA.Ab_User_bA.Ab_symbol_bA += amount;
		let vec = Ab_GameDC_bA.Ab_reapSymbolVec_bA;

		let light = this.Ab_ui_symbol_bA.node.getChildByName('light');
		let bubble = this.Ab_ui_symbol_bA.node.getChildByName('bubble');

		let scale = v3(1.2, 1.2, this.Ab_ui_symbol_bA.node.scale.z);
		let one_scale = v3(1, 1, this.Ab_ui_symbol_bA.node.scale.z);
		if(vec) Ab_Float_bA.Ab_Reap_bA(this.Ab_icon_symbol_bA, abi.Ab_number_bA.Ab_random_bA(6, 8), vec, (n, i)=>{
			Ab_AudioManager_bA.Ab_playEffect_bA(Ab_AudioManager_bA.Ab_EAudio_bA.Ab_EF_SYMBOL_bA);
			Tween.stopAllByTarget(this.Ab_ui_symbol_bA.node);
			this.Ab_ui_symbol_bA.node.setScale(one_scale);
			tween(this.Ab_ui_symbol_bA.node).to(0.1, {scale}).to(0.1, {scale: one_scale}).start();

			if(!light.active){
				light.angle = 0;
				light.active = true;
				Tween.stopAllByTarget(light);

				tween(light)
				.by(1, {angle: 180})
				.repeatForever()
				.start();
			}

			if(!bubble.active){
				bubble.active = true;
				Tween.stopAllByTarget(bubble);
				bubble.setPosition(550, 0, bubble.position.z);
				tween(bubble)
				.to(0.3, {position: v3(580, 0, bubble.position.z)}, {easing: 'cubicOut'})
				.to(0.3, {position: v3(550, 0, bubble.position.z)}, {easing: 'cubicIn'})
				.union().repeat(3).delay(0.6)
				.union().repeat(3).call(()=>{
					Tween.stopAllByTarget(bubble);
					bubble.active = false;
				}).start();
			}
		}).then(()=>{
			this.Ab_lbl_symbol_bA.string = Ab_Config_bA.Ab_symbolString_bA(Ab_GameDC_bA.Ab_User_bA.Ab_symbol_bA);
			let opt = (this.Ab_symbol_pool_bA.get() ?? instantiate(this.Ab_symbol_demo_bA)).getComponent(UIOpacity);

			opt.node.getChildByName('lbl-value').getComponent(Label).string = '+' + Ab_Config_bA.Ab_symbolString_bA(amount);
			opt.node.setParent(this.Ab_ui_symbol_bA.node);
			[opt, opt.node].forEach(el=>Tween.stopAllByTarget(el));

			opt.opacity = 255;
			opt.node.setPosition(35, 0);

			tween(opt).delay(0.3).to(0.7, {opacity: 0}).start();

			tween(opt.node).by(1, {position: v3(0, 115, 0)})
			.call(()=>this.Ab_symbol_pool_bA.put(opt.node))
			.start();

			if(light.active){
				Tween.stopAllByTarget(light);
				light.active = false;
				light.angle = 0;
			}
		});
		else this.Ab_lbl_symbol_bA.string = Ab_Config_bA.Ab_symbolString_bA(Ab_GameDC_bA.Ab_User_bA.Ab_symbol_bA);
	}

	private async Ab_initGuide_bA(){

		let np_guide = new abi.Ab_NudityPromise_bA<'end'>();
		if(Ab_GameDC_bA.Ab_Status_bA.Ab_guideInited_bA) np_guide.Ab_resolve_bA('end');
		else{
			let reg = Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_Status_bA, 'Ab_guideInited_bA', ()=>{
				Ab_DataTransfer_bA.Ab_cancel_bA(reg);
				np_guide.Ab_resolve_bA('end');
			}, false);
		}
		await np_guide.Ab_promise_bA;

		let btn_free = this.Ab___btn_map___bA.get('free');
		let ui_guide_symbol = this.Ab_ui_top_bA.node.getChildByName('guide-symbol').getComponent(UITransform);
		let ui_btn_free = btn_free.getComponent(UITransform);

		if(!Ab_GameDC_bA.Ab_checkGuideID_bA(5)){
			Ab_Guide_bA.Ab_Create_bA(5,
				Ab_GameAssets_bA.Ab_getLangText_bA(55),
				ui_guide_symbol,
				ui_btn_free
			);

			Ab_Guide_bA.Ab_Wait_bA(4, false).then(()=>Ab_Guide_bA.Ab_Show_bA(5));

			Ab_Guide_bA.Ab_Wait_bA(5, true).then(()=>{
				btn_free.interactable = false;
				Ab_Popup_bA.Ab_Free_bA().then(()=>btn_free.interactable = true);
			});
		}

		if(!Ab_GameDC_bA.Ab_checkGuideID_bA(6)){
			Ab_Guide_bA.Ab_Create_bA(6,
				Ab_GameAssets_bA.Ab_getLangText_bA(53),
				ui_guide_symbol,
				ui_btn_free
			);
		}
	}
}

